/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.ejb;

import entities.Fantasyuser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author The Lunatics
 */
@Stateless
public class FantasyuserFacade extends AbstractFacade<Fantasyuser> {
    @PersistenceContext(unitName = "Touchdown308PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public FantasyuserFacade() {
        super(Fantasyuser.class);
    }
    
    public Fantasyuser getUserByLoginName(String loginName, String password)
    {
        Query createNamedQuery = getEntityManager()
                                     .createNamedQuery("Fantasyuser.findByLoginName");
        
        createNamedQuery.setParameter("loginName", loginName);
        
        if (createNamedQuery.getResultList()
                                .size() > 0) {
            return (Fantasyuser) createNamedQuery.getSingleResult();
        } else {
            return null;
        }
    }
    
    public Fantasyuser getUserById(String id)
    {
        Integer i = new Integer(id);
        
        Query createNamedQuery = getEntityManager()
                                     .createNamedQuery("Fantasyuser.findById");
        
        createNamedQuery.setParameter("id", i);
        
        if (createNamedQuery.getResultList()
                                .size() > 0) {
            return (Fantasyuser) createNamedQuery.getSingleResult();
        } else {
            return null;
        }
    }
    
    
    
    public Fantasyuser getUserByEmail(String email)
    {
        Query createNamedQuery = getEntityManager()
                                     .createNamedQuery("Fantasyuser.findByEmail");
        
        createNamedQuery.setParameter("email", email);
        
        if (createNamedQuery.getResultList()
                                .size() > 0) {
            return (Fantasyuser) createNamedQuery.getSingleResult();
        } else {
            return null;
        }
    }
    
    public List getUserList()
    {
         Query createQuery = getEntityManager()
                 .createQuery("select f.email from Fantasyuser f");
        
         List l = createQuery.getResultList();
         
         return l;
        
    }
    
    public List getAllUserList(){
        Query createNamedQuery = getEntityManager()
                                     .createNamedQuery("Fantasyuser.findAll");
        return createNamedQuery.getResultList();
    }
    
    public boolean isLoginNameOccupied(String loginName){
        Query createNamedQuery = getEntityManager()
                                     .createNamedQuery("Fantasyuser.findByLoginName");
        createNamedQuery.setParameter("loginName", loginName);
        
        return (createNamedQuery.getResultList().size() > 0);
    }
    
    public boolean isEmailOccupied(String email){
        Query createNamedQuery = getEntityManager()
                                     .createNamedQuery("Fantasyuser.findByEmail");
        createNamedQuery.setParameter("email", email);
        
        return (createNamedQuery.getResultList().size() > 0);
    }
    
    public void createNewAccount(Fantasyuser newAccount){
        em.persist(newAccount);
    }
}
